import {
  ajaxFun
} from "../../../utils/ajax";


Page({
  data: {
    addressDesc: {}, //地址信息
    goodsList: []//商品列表
  },
  onLoad(options) {
    let cartItemIds = JSON.parse(options.cartItemIds);
    //  console.log(cartItemIds);
    let str = cartItemIds.join(",");
    console.log(str);

    this.getGoodsList(str);
    this.getDefaultAddress()
  },

  async getGoodsList(cartItemIds) { //获取商品详情
    let result = await ajaxFun("/shop-cart/settle", "get", {
      cartItemIds
    });
    this.setData({
      goodsList: result.data
    })

  },
  async getDefaultAddress() {
    let result = await ajaxFun("/address/default", "get");
    this.setData({
      addressDesc: result.data
    })
  },
  async submitOrder(){
    let arry=[];
    this.data.goodsList.map(item=>{
        arry.push(item.cartItemId)
    })
    let result=await ajaxFun("/saveOrder","post",{
      "addressId": this.data.addressDesc.addressId,//地址id
      "cartItemIds":arry
    })
    // 提交完成订单 修改购物车的数据渲染
    if(result.resultCode===200){
      let pages = getCurrentPages();//当前页面
      let prePage = pages[pages.length - 2];//上一个页面
      let newArry=prePage.data.goodsList;
      let newArryTwo= newArry.filter(item=>item.blo==false);
      prePage.setData({
        goodsList:newArryTwo
      })
    }
  }
})